package com.yxzr.youyou;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hjq.baselibrary.base.BaseDialog;
import com.yxzr.youyou.adapter.EarnPointsAdapter;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.SignBean;
import com.yxzr.youyou.bean.UserInfoBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class EarnPointsActivity extends BaseActivity {
    private TextView earnpoints_tv_url;
    private RecyclerView mRecyclerView;
    private Button earnpoints_btn;
    private EarnPointsAdapter adapter;
    private SharedPreferencesHelper sp;
    private UserInfoBean.DataBean userInfoBean;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_earnpoints);
        sp = new SharedPreferencesHelper(this,"logindata");
        userInfoBean = (UserInfoBean.DataBean) getIntent().getSerializableExtra("userinfo");
        setTitleWithBack();
        initView();
    }

    private void initView() {
        earnpoints_tv_url = findViewById(R.id.earnpoints_tv_url);
        mRecyclerView = findViewById(R.id.earnpoints_rv);
        earnpoints_btn = findViewById(R.id.earnpoints_btn);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this,3);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new RecyclerItemDecoration(20,3));
        adapter = new EarnPointsAdapter(this);
        mRecyclerView.setAdapter(adapter);

        earnpoints_tv_url.setText(userInfoBean.getQrcode_url());
        earnpoints_tv_url.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                new BaseDialog.Builder(EarnPointsActivity.this)
                        .setContentView(R.layout.setting_dialog_layout)
                        .setWidth(800)
                        .setHeight(500)
                        .setText(R.id.text,"确定要复制吗？")
                        .setOnClickListener(R.id.close, new BaseDialog.OnClickListener() {
                            @Override
                            public void onClick(BaseDialog dialog, View view) {
                                dialog.dismiss();
                            }
                        })
                        .setOnClickListener(R.id.shenqing, new BaseDialog.OnClickListener() {
                            @Override
                            public void onClick(BaseDialog dialog, View view) {
                                dialog.dismiss();
                                ClipboardManager clip = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                                clip.setText(earnpoints_tv_url.getText().toString().trim()); // 复制
                                Toast.makeText(EarnPointsActivity.this,"复制成功",Toast.LENGTH_LONG).show();
                            }
                        })
                        .show();

                return true;
            }
        });

        earnpoints_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
            }
        });

        adapter.setSelectIndex(Integer.parseInt(userInfoBean.getQiandao()));
    }

    private void initData(){
        RequestUtil.getInstence().API().sign((String) sp.getSharedPreference("token",""))
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        SignBean bean = gson.fromJson(t,SignBean.class);

                        adapter.setSelectIndex(Integer.parseInt(bean.getData().getDay()));
                    }

                    @Override
                    protected void onFail(int error_code) {
                        if (error_code == 201){
                            new BaseDialog.Builder(EarnPointsActivity.this)
                                    .setContentView(R.layout.content_dialog_layout)
                                    .setWidth(800)
                                    .setHeight(500)
                                    .setText(R.id.text,"今天已签到")
                                    .setOnClickListener(R.id.shenqing, new BaseDialog.OnClickListener() {
                                        @Override
                                        public void onClick(BaseDialog dialog, View view) {
                                            dialog.dismiss();

                                        }
                                    })
                                    .show();
                        }
                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
